package com.rp.system.dao;

import java.util.List;

import org.raptor.common.DbOperUtil;
import org.raptor.orm.hibernate.DbaHibernateTemplateSupport;
import org.raptor.pagination.Page2;
import org.springframework.stereotype.Repository;

import com.rp.common.utils.RpUtil;
import com.rp.common.vo.PageParam;
import com.rp.system.po.Resource;


@Repository
public class ResourceDao extends DbaHibernateTemplateSupport<Resource> {
	
	public static final String GET_RESOURCES_BY_AUTHORITYID = "select elements(a.resources) from Authority a where a.id = ?";
	
	public static final String INSERT_RESOURCE = "INSERT INTO RP_SYSTEM_RESOURCES " +
			"(id,createDate,updateDate,version,name,remark,type,url,moduleId,moduleName) " +
			"VALUES (?,?,?,?,?,?,?,?,?,?)";
	
	/**
	 * 获取所有资源
	 * @return
	 */
	public List<Resource> getAllResources(){
		return this.find("from Resource" + DbOperUtil.generateOrderBy(null, null));
	}
	
	/**
	 * 根据权限ID获取权限对应的资源
	 * @param authorityId
	 * @return
	 * @throws Exception 
	 */
	public Page2 getResourcesByAuthorityId(PageParam pageParam, String authorityId) throws Exception{
		Page2 page = null;
		if(authorityId == null){
			throw new Exception("authorityId不能为空");
		}else{
			page = this.getPage2ByOffset(GET_RESOURCES_BY_AUTHORITYID, 
					pageParam.getPageOffset(), pageParam.getPageSize(),
					pageParam.getSortField(), pageParam.getSortOrder(), authorityId);
		}
		
		return page;
	}
	
	/**
	 * 根据权限ID获取权限对应的资源，直接获取所有并返回List列表，不分页
	 * @param authorityId
	 * @return
	 * @throws Exception
	 */
	public List<Resource> getResourcesByAuthorityId(String authorityId) throws Exception{
		if(authorityId == null){
			throw new Exception("authorityId不能为空");
		}else{
			return this.find(GET_RESOURCES_BY_AUTHORITYID, authorityId);
		}
	}
	
	
	/**
	 * 分页获取资源数据
	 * @param pageParam
	 * @param moduleId
	 * @return
	 */
	public Page2 getResourcesByPage(PageParam pageParam, String moduleId, Resource resource){
		String name = resource.getName();
		if(RpUtil.isNullOrEmpty(name)){
			name = "";
		}
		Page2 page = null;
		if(moduleId == null){
			page = this.getPage2ByOffset("from Resource where name like ?", 
					pageParam.getPageOffset(), pageParam.getPageSize(),
					pageParam.getSortField(), pageParam.getSortOrder(),
					DbOperUtil.fuzzyQueryParam(name));
		}else{
			page = this.getPage2ByOffset("from Resource where moduleId = ? and name like ?", 
					pageParam.getPageOffset(), pageParam.getPageSize(),
					pageParam.getSortField(), pageParam.getSortOrder(), 
					moduleId, DbOperUtil.fuzzyQueryParam(name));
		}
		return page;
	}
	
	/**
	 * 分页获取资源数据，传入权限ID，排除此权限已分配有的资源
	 * @param pageParam
	 * @param moduleId 模块ID，如果传空，则获取所有模块下的资源
	 * @param authorityId 目标权限ID
	 * @return
	 */
	public Page2 getResourcesByPage2(PageParam pageParam, String moduleId, String authorityId) {
		List<Resource> hasResources = this.find(GET_RESOURCES_BY_AUTHORITYID, authorityId);
		
		Page2 page = null;
		if(moduleId == null){
			page = this.getPage2ByOffset("from Resource", 
					pageParam.getPageOffset(), pageParam.getPageSize(),
					pageParam.getSortField(), pageParam.getSortOrder());
		}else{
			page = this.getPage2ByOffset("from Resource where moduleId = ?", 
					pageParam.getPageOffset(), pageParam.getPageSize(),
					pageParam.getSortField(), pageParam.getSortOrder(), moduleId);
			
		}
		List<Resource> allRescs = (List<Resource>) page.getData();
		for(int i=0; i<allRescs.size(); i++){
			if(hasResources.contains(allRescs.get(i))){
				allRescs.remove(i);
				i--;
			}
		}
		page.setData(allRescs);
		return page;
	}
	
	
}
